%macro sample_select ; 

/* this macro creates CTE flag  */
%macro cte_flag ; 

degcip_2dig in ('10','11','12','13','15','19',
                '43','44','46','47','48','49','51','52') 


%mend ;

proc contents data=INPUTS.pseo_tx_thecb_2000_2019_deg_rsch ;
	title 'grads';
run;

data graduates (keep= pik degcip_2dig opeid year_grad quarter_grad qtime_grad
					deglevl_code state flag_cte) /view=graduates;

    merge INPUTS.pseo_tx_thecb_2000_2019_deg_rsch (in=a drop=year_grad) 
          INPUTS.pseo_tx_thecb_2000_2019_deg_pik (in=b) ;
    by dqb_source_id ; 
    
    state = '48' ;
    degcip_2dig = substr(degcip,1,2) ;
    opeid = substr(opeid,3,6)||'00' ;
    flag_cte = %cte_flag;
	
	if length(date_grad) = 6 then date_grad = trim(date_grad)||'01' ;
	else if length(date_grad) = 4 then date_grad = trim(date_grad)||'1201' ;
	date_grad_num = input(date_grad,yymmdd10.) ;
    
    quarter_grad = qtr(date_grad_num) ;
    year_grad = year(date_grad_num) ;
    qtime_grad = %qtime(year_grad,quarter_grad,location=datastep) ;
    
    
    if deglevl_code in ('01','02','03','04') ;
    
    if year_grad>=2001 ; 
run;

proc contents data=graduates ;
	title 'graduates contents' ;
run;

proc freq data=INPUTS.pseo_tx_thecb_2000_2019_enr_rsch ; 
	tables enrsem/missing ; 
	title 'Terms' ;
run;

proc sort data=INPUTS.pseo_tx_thecb_2000_2019_enr_qpik out=qpik ;
	by dqb_source_id ; 
run;

proc sort data=INPUTS.pseo_tx_thecb_2000_2019_enr_rsch out=rsch ;
	by dqb_source_id ; 
run;

proc contents data=rsch ; 
	title 'contents of research dataset' ;
run;

proc print data=rsch (obs=20) ;
	title 'observations and seeing credit hours' ;
run;

data nongraduates (keep = pik degcip_2dig opeid  credithours year 
						quarter state cte_flag deglevl_code )
    ;
    
    merge rsch (in=a rename=(enryear=year 
							enrtotsch = credithours)) 
          qpik (in=b rename=(qpik_ssn = pik )) ;
    by dqb_source_id ; 
    length opeid $8. ; 
    state = '48' ; 
	opeid = substr(opeid,3,6)||'00' ;
	deglevl_code = enrlev ;
	
    /* need to add qtimes */
		
	if enrsem = 1 then quarter = 4 ;
	if enrsem = 2 then quarter = 2 ; 
	if enrsem = 3 then quarter = 3 ;
	if enrsem = 4 then quarter = 3 ; 
	degcip_2dig = substr(enrcip,1,2) ;
    cte_flag=%cte_flag;
    
    if year>=2001 & deglevl_code in ('01','02','03','04') ;
		if enrsem = 2 then do ; 
		output ;
		quarter = 1 ; 
		output ; 
	end ;
	else output ; 
run;


     /* need to get max credit hours */ 
proc sort data=nongraduates out=nongrads; 
	by pik opeid deglevl_code year quarter ; 
run;

proc contents data=nongrads ; 
run;
proc print data=nongrads (obs=10) ;
	var pik opeid deglevl_code year quarter credithours ; 
run;

proc freq data=nongrads ; 
	tables quarter/missing ; 
	title 'Quarters' ;
run;

proc print data=nongrads (obs=10) ;
	var pik opeid deglevl_code year quarter credithours cumulative_hrs; 
run;
    
proc summary data=nongrads nway ;
    class pik opeid deglevl_code ;
    var credithours cte_flag;
    output out=maxcrd (drop = _TYPE_ _FREQ_) 
            sum(credithours) = maxcrd 
            max(cte_flag)= cte_flag;
run;

proc freq data=nongrads ; 
	tables cte_flag ;
	title 'cte flag in nongrads' ;
run;
    
proc sort data=nongrads ;
    by pik opeid deglevl_code ; 
run;
    
proc print data=maxcrd (obs=50) ; 
	title 'maxcrd observations' ;
run;

data nongraduates_finalenroll (drop=credithours); 
    merge nongrads (in=a drop=cte_flag) maxcrd (in=b) ;
    by pik opeid deglevl_code;
    
    if maxcrd >=10 ;
            
    if last.deglevl_code and cte_flag = 1 then output; 
run;
            
proc sort data=graduates out=grads nodupkey ;
     by pik opeid deglevl_code ; 
run;
            
proc freq data=grads ;
      title 'CTE FLAG GRADS' ;
      tables flag_cte ; 
run;

proc contents data=grads; 
run;
proc contents data=nongraduates_finalenroll;
run;

data OUTPUTS.all_students_thecb (drop=maxcrd cte_flag); 
       merge grads (in=a) 
             nongraduates_finalenroll ; 
       by pik opeid deglevl_code ; 
            
       graduate = a ; 
            
       if (flag_cte and graduate) or not graduate ;
run;

%mend sample_select;  
%sample_select;
